﻿using JumpstartApp.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace JumpstartApp.Services
{
    using JumpstartApp.Models;
    using JumpstartApp.Views;

    public static class Navigation
    {
        static Frame s_Frame = null;
        public static Frame Frame
        {
            get
            {
                if (s_Frame == null)
                {
                    Window.Current.Content = s_Frame = (Window.Current.Content as Frame) ?? new Frame();
                    Window.Current.Activate();
                }
                return s_Frame;
            }
        }

        public static void GotoHome(bool clear = false)
        {
            if (!Frame.Navigate(typeof(AppHub)))
            {
                // Debugger.Break();
            }
            if (clear)
                Frame.SetNavigationState(new Frame().GetNavigationState());
        }

        public static void GotoSearch(string text)
        {
            if (!Frame.Navigate(typeof(Contracts.Search), text))
                Debugger.Break();
        }

        public static void GotoDetail(ModuleRecord record) { GotoDetail(record.Id); }
        public static void GotoDetail(EpisodeRecord record) { GotoDetail(record.Id); }

        public static void GotoDetail(string id)
        {
            if (Frame.Content is Views.Detail && Frame.CanGoBack)
                Frame.GoBack();
            if (!Frame.Navigate(typeof(Views.Detail), id))
                Debugger.Break();
        }

        internal static void GotoSplash() { Frame.Navigate(typeof(Views.Splash)); }
        internal static void GotoCamera() { Frame.Navigate(typeof(Views.Camera)); }
        internal static void GotoFail(string text)
        {
            Frame.Navigate(typeof(Views.Fail), text);
        }

        public static void Reload()
        {
            Frame.Navigate(Frame.Content.GetType());
            Frame.GoBack();
        }

        public static void SaveState() { State = Frame.GetNavigationState(); }

        public static void RestoreState()
        {
            if (!string.IsNullOrEmpty(Services.Navigation.State))
                Frame.SetNavigationState(State);
        }

        static readonly string SuspendNavigationKey = "App.NavigationState";
        static readonly IPropertySet Settings = ApplicationData.Current.LocalSettings.Values;
        private static string State
        {
            get
            {
                return (Settings.ContainsKey(SuspendNavigationKey)) ?
                    Settings[SuspendNavigationKey].ToString() : string.Empty;
            }
            set { Settings[SuspendNavigationKey] = value; }
        }

        internal static void GoBack()
        {
            if (Frame.CanGoBack)
                Frame.GoBack();
            else
                // never fail, also go home
                Frame.Navigate(typeof(AppHub));
        }
    }
}
